<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        .box {
            width: 375px;
            height: 600px;
            box-sizing: border-box;
            padding: 10px;
            margin: 50px auto;
            background-color: lightgoldenrodyellow;
        }

        #app {
            margin: 0 10px;
        }

        #app .input-box {
            margin-bottom: 20px;
            display: flex;
            align-items: center;
        }

        #app .input-box input {
            flex: 1;
            height: 35px;
            border: 1px solid grey;
            outline: none;
            border: none;
            padding-left: 20px;
            background-color: #f5f5f5;
        }

        #app .input-box span {
            text-align: center;
            color: cornflowerblue;
            width: 60px;
            cursor: pointer;
        }

        #app .title-box {
            margin-bottom: 20px;
        }

        #app .title-box span {
            margin-right: 5px;
            padding: 5px 5px;
        }

        #app .title-box span {
            margin-right: 5px;
            padding: 5px 7px;
            background-color: lightgreen;
            font-size: 14px;
            color: white;
            border-radius: 3px;
        }

        #app .title-box .pointed {
            margin-right: 5px;
            padding: 5px 5px;
            background-color: coral;
        }

        #app .title-box span:last-child {
            background-color: coral;
        }

        #app .list>span {
            font-weight: 700;
        }

        #app .list .task {
            margin: 20px 0;
            display: flex;
            justify-content: space-between;
            padding: 10px;
            background-color: lightgreen;
            color: white;
        }

        #app .list .task div {
            box-sizing: border-box;
            padding: 5px;
            color: white;
            display: flex;
        }

        #app .list .task div input {
            margin-right: 15px;
        }

        #app .list .task div .number {
            margin-right: 10px;
        }

        #app .list .task .btn {
            cursor: pointer;
        }


        #app .list .task.finished {
            background-color: coral;
        }

        #app .empty {
            text-align: center;
            margin: 50px 0;
            font-size: 14px;
        }
    </style>
</head>

<body>
    <div class="box">
        <div id="app">
            <div class="input-box">
                <input type="text" placeholder="请输入任务名称" v-model="inputText">
                <span @click="add">新增</span>
            </div>
            <div class="title-box">
                <span>全部任务 ( {{ tasklist.length }} )</span>
                <span>已完成 ( {{ achieveCount }} )</span>
                <span>未完成 ( {{ unachieveCount }} )</span>
            </div>
            <div class="list">
                <span>任务列表</span>
                <div :class=" `task ${item.status?'finished':''}` " v-for="(item,index) in tasklist" :key="index">
                    <div>
                        <input type="checkbox" @click="changeStatus(item)">
                        <div class="number">{{item.id}}</div>
                        <div class="name">{{item.name}}</div>
                    </div>
                    <div class="btn" @click="del(item,index)">删除</div>
                </div>
            </div>
            <div class="empty" v-show="tasklist.length == 0">任务列表为空,请添加一个任务!</div>
        </div>
    </div>
</body>

</html>
<script src="./vue-2.5.21.js"></script>
<script>
    var v = new Vue({
        //挂载到dom节点上
        el: '#app',
        data: {

            inputText: '',
            tasklist:
                [

                ],
            achieveCount: 0,
            unachieveCount: 0
        },
        methods: {
            //添加任务
            add() {
                //判断输入框里有无内容
                if (this.inputText) {

                    var obj = { id: this.tasklist.length + 1, name: this.inputText, status: false, count: this.tasklist.length };

                    this.tasklist.push(obj);
                    //执行完清空输入框
                    this.inputText = ''
                }
            },
            //更改任务状态
            changeStatus(item) {
                item.status = !item.status;
            },
            //删除
            del(item, index) {
                this.tasklist.splice(index, 1)
            },
            // //计数变化
            // achieved() {
            //     var count = 0;
            //     this.tasklist.map((item, index) => {
            //         if (item.status == true) {
            //             count++
            //         }
            //     })
            //     return count
            // },
            // unachieved() {
            //     var count = 0;
            //     this.tasklist.map((item, index) => {
            //         if (item.status == false) {
            //             count++
            //         }
            //     })
            //     return count
            // }
        },
        computed: {
            // achieveCount(){
            //     return this.tasklist.filter((item)=>{
            //         return item.status
            //     }).length
            // },
            // unachieveCount(){
            //     return this.tasklist.length-this.achieveCount;
            // },
        },
        watch: {//定义数据监听
            // 写法一: 函数的形式 可以监听到数据本身的改变, 无法监听到内部数据属性的改变
            // tasklist(){
            //     //已完成任务的数量计算
            //     this.achieveCount = this.tasklist.filter((item)=>{
            //         return item.status}).length
            //     //未完成任务的数量计算
            //     this.unachieveCount = this.tasklist.length - this.tasklist.filter((item)=>{ return item.status }).length;
            // }

            // 写法二: 对象的形式+深度监听 可以监听到内部数据属性的改变
            tasklist:{
                handler(){
                 //计算已完成任务的数量
                 this.achieveCount = this.tasklist.filter((item)=>{
                return item.status }).length;
                   //计算未完成任务的数量
                   this.unachieveCount = this.tasklist.length - this.tasklist.filter((item)=>{return item.status}).length 
                },
                deep:true,
            }
        }
    })
</script>